package event

import (
	"context"
	"time"
)

type BootClient struct {
	*Event
	ctx    context.Context
	cancel context.CancelFunc
}

func NewBootClient() *BootClient {
	return &BootClient{Event: NewEvent()}
}

func NewBootClientContext(ctx context.Context) *BootClient {
	return &BootClient{ctx: ctx}
}

func (b *BootClient) CancelFunc() {
	if b.cancel != nil {
		b.cancel()
	}
}

func (b *BootClient) Dial(network string, addr string) (StarterClient, error) {
	return b.DialTimeout(network, addr, time.Second*30)
}

func (b *BootClient) DialTimeout(network string, addr string, timeout time.Duration) (StarterClient, error) {
	client := newStarterClient(network, b.Event, b.ctx)
	if _, err := client.DialTimeout(addr, timeout); err != nil {
		return nil, err
	}
	return client, nil
}

func (b *BootClient) DialTcp(addr string) (StarterClient, error) {
	return b.DialTimeout("tcp", addr, time.Second*30)
}

func (b *BootClient) DialTcpTimeout(addr string, timeout time.Duration) (StarterClient, error) {
	return b.DialTimeout("tcp", addr, timeout)
}

func (b *BootClient) DialUdp(addr string) (StarterClient, error) {
	return b.DialTimeout("udp", addr, time.Second*30)
}

func (b *BootClient) DialUdpTimeout(addr string, timeout time.Duration) (StarterClient, error) {
	return b.DialTimeout("udp", addr, timeout)
}
